package com.mengshu.platform.web.controller;

import com.mengshu.mybatisplus.basic.BasicController;
import com.mengshu.mybatisplus.validator.ValidatorGroup;
import com.mengshu.platform.config.properties.PlatformProperties;
import com.mengshu.platform.po.AdminUserPO;
import com.mengshu.platform.service.AdminUserService;
import com.mengshu.platform.utils.PasswordUtils;
import com.mengshu.platform.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *     用户管理
 * </p>
 *
 * @author liuning
 * @date 1:56 下午 2022/2/5
 */
@RestController
@RequestMapping("admin/user")
@RequiredArgsConstructor
public class AdminUserController extends BasicController<AdminUserService, AdminUserPO> {

    private final PlatformProperties platformProperties;

    @Override
    @PostMapping("save")
    public boolean save(@RequestBody @Validated(ValidatorGroup.Save.class) AdminUserPO user) {
        String password = user.getPassword();
        if (!StringUtils.hasText(password)) {
            password = platformProperties.getDefaults().getPassword();
        }
        String salt = StringUtils.uuid();
        user.setSalt(salt);
        user.setPassword(PasswordUtils.SHA256(password, salt));
        return super.save(user);
    }

}
